package model

import (
	"golang_template/global"
	"gorm.io/gorm"
)

// User model
type User struct {
	Account     string `json:"account" form:"account" gorm:"column:account"`
	Password    string `json:"password" form:"password" gorm:"column:password"`
	Username    string `json:"username" form:"username" gorm:"column:username;default:新用户"`
	Tag         string `json:"tag" form:"tag" gorm:"column:tag;default:天天向上"`
	CommonModel `gorm:embedded`
}

func (User) TableName() string {
	return "user"
}

func (User) Find(account string) (*gorm.DB, User) {
	var user User
	tx := global.DB.Where("account = ?", account).First(&user)
	return tx, user
}

func (u User) Create() *gorm.DB {
	tx := global.DB.Create(&u)
	return tx
}

// Update 更新用户信息 提供基础的修改username
func (User) Update(username string) *gorm.DB {
	tx := global.DB.Model(User{}).Update("username", username)
	return tx
}

//例子
//	func (User) FindToLogin(account string) (*gorm.DB, User) {
//		var user User
//		//var outLogin []responseDto.LoginResponse
//		tx := global.DB.Where("account = ?", account).First(&user)
//		//global.DB.Table("user").
//		//	Select("user.id as id,user.password as password,userInfo.user_name as username,userInfo.gender as gender").
//		//	Joins("inner join user_info userInfo on userInfo.id = user.user_info_id").Where("user.account = ?", account).Scan(&outLogin)
//		//fmt.Printf("%#v", outLogin)
//		return tx, user
//	}
